<?php

namespace app\store\controller\campus;

use app\store\controller\Controller;
use app\store\model\campus\Campus as CampusModel;
use app\store\model\campus\CoursesCategory as CampusCoursesCategoryModel;
use app\store\model\CoursesCategory as CoursesCategoryModel;

/**
 * 校区专业控制器
 * Class Teacher
 * @package app\store\controller\campus
 */
class CoursesCategory extends Controller
{
    /**
     * 专业列表
     * @param int $campus_id
     * @return mixed
     * @throws \think\exception\DbException
     */
    public function index($campus_id = 0)
    {
        // 专业列表
        $model = new CampusCoursesCategoryModel;
        $list = $model->getList($campus_id);
        return $this->fetch('index', compact('list' ));
    }

    /**
     * 添加专业
     * @return array|bool|mixed
     * @throws \Exception
     */
    public function add()
    {
        $model = new CampusCoursesCategoryModel;
        if (!$this->request->isAjax()) {
            // 校区列表
            $campusList = CampusModel::getAllList();
            $coursesCategoryList = CoursesCategoryModel::getCacheTree();

            return $this->fetch('add', compact('campusList','coursesCategoryList'));
        }
        // 新增记录
        if ($model->add($this->postData('courses_category'))) {
            return $this->renderSuccess('添加成功', url('campus.courses_category/index'));
        }
        return $this->renderError($model->getError() ?: '添加失败');
    }

    /**
     * 编辑专业
     * @param $id
     * @return array|bool|mixed
     * @throws \think\exception\DbException
     */
    public function edit($id)
    {
        // 专业详情
        $model = CampusCoursesCategoryModel::detail($id);

        if (!$this->request->isAjax()) {
            // 校区列表
            $campusList = CampusModel::getAllList();
            $coursesCategoryList = CoursesCategoryModel::getCacheTree();
            return $this->fetch('edit', compact('model', 'campusList','coursesCategoryList'));
        }
        // 新增记录
        if ($model->edit($this->postData('courses_category'))) {
            return $this->renderSuccess('更新成功', url('campus.courses_category/index'));
        }
        return $this->renderError($model->getError() ?: '更新失败');
    }

    /**
     * 删除专业
     * @param $id
     * @return array
     * @throws \think\exception\DbException
     */
    public function delete($id)
    {
        // 专业详情
        $model = CampusCoursesCategoryModel::detail($id);
        if (!$model->setDelete()) {
            return $this->renderError($model->getError() ?: '删除失败');
        }
        return $this->renderSuccess('删除成功');
    }

}